<?php

namespace Data;

class Hello extends Base {

    public function addHello($userId, $toUserId, $content) {
        $data = array(
            'UserId' => $userId,
            'ToUserId' => $toUserId,
            'Content' => $content,
            'CreateTime' => time(),
            'UpdateTime' => time(),
            'Status' => self::IS_YES,
            'IsRead' => self::IS_NO,
            'SendIsRead' => self::IS_YES,
        );
        $this->insert($data);
    }

    public function getUserNotReadHelloCount($userId) {
        $condition = array(
            'Status' => self::IS_YES,
            'or' => array(
                array(
                    'ToUserId' => $userId,
                    'IsRead' => self::IS_NO,
                ),
                array(
                    'UserId' => $userId,
                    'SendIsRead' => self::IS_NO,
                ),
            ),
        );
        $count = $this->where($condition)->count();
        return $count;
    }

    public function getUserHello($userId, $pager) {
        $condition = array(
            'Status' => self::IS_YES,
            'or' => array(
                array(
                    'ToUserId' => $userId,
                ),
                array(
                    'UserId' => $userId,
                ),
            ),
        );
        $message = $this->where($condition)->order('UpdateTime desc')->pager($pager)->findAll();
        return $message;
    }

    public function getHelloByIdAndUserId($helloId, $userId) {
        $condition = array(
            'HelloId' => $helloId,
            'Status' => self::IS_YES,
            'or' => array(
                array(
                    'ToUserId' => $userId,
                ),
                array(
                    'UserId' => $userId,
                ),
            ),
        );
        $hello = $this->where($condition)->findOne();
        return $hello;
    }

    public function getHello($helloId) {
        $condition = array(
            'HelloId' => $helloId,
            'Status' => self::IS_YES,
        );
        $hello = $this->where($condition)->findOne();
        return $hello;
    }

    public function setHelloViewEd($helloId, $userId) {
        $condition = array(
            'HelloId' => $helloId,
            'ToUserId' => $userId,
            'Status' => self::IS_YES,
        );
        $this->where($condition)->update(array('IsRead' => self::IS_YES));
    }

    public function updateReadStatus($helloId, $field, $status) {
        $this->where(array('HelloId' => $helloId))->update(array($field => $status));
    }

}